package com.cskaoyan.controller.admin.systemcontroller;

import com.cskaoyan.Utils.LogUtils;
import com.cskaoyan.Utils.MyUtils;
import com.cskaoyan.bean.systembean.Admin;
import com.cskaoyan.service.systemservice.AdminService;
import com.cskaoyan.vo.BaseRespVo;
import com.cskaoyan.vo.systemvo.ListVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/***
 * author:zhw
 * date:5.10
 */
@RestController
@RequestMapping("admin/admin/")
public class AdminController {
    @Autowired
    AdminService adminService;
    @Autowired
    LogUtils logUtils;
    @RequiresPermissions("admin:admin:list")
    @RequestMapping("list")
    public BaseRespVo admin(Integer page,Integer limit,String username,String sort,String order){
        ListVo listVo = adminService.list(page,limit,username,sort,order);
        return BaseRespVo.ok(listVo);


    }

    /**
     * 添加管理员
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:create")
    @RequestMapping("create")
    public BaseRespVo create(@RequestBody Admin admin, HttpServletRequest request){
//        String host = SecurityUtils.getSubject().getSession().getHost();
        Admin admin1 = adminService.create(admin);
        String remoteAddr = request.getRemoteAddr();
        logUtils.insertLog(MyUtils.getAdminname(),remoteAddr,new Date(),1,"添加管理员",true,admin.getUsername());
        return BaseRespVo.ok(admin1);
    }

    /**
     * TODO 更新时间上可能有瑕疵
     * 根据id更新 username password avatar updateTime roleIds
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:update")
    @RequestMapping("update")
    public BaseRespVo update(@RequestBody Admin admin,HttpServletRequest request){
        Admin admin1 = adminService.update(admin);
        logUtils.insertLog(MyUtils.getAdminname(),request.getRemoteAddr(),new Date(),1,"编辑管理员",true,admin.getUsername());
        return BaseRespVo.ok(admin1);
    }

    /**
     * 根据id删除
     * @param admin
     * @return
     */
    @RequiresPermissions("admin:admin:delete")
    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Admin admin){
        adminService.delete(admin);
        return BaseRespVo.ok();
    }



}
